/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.muni.fi.pb138.webrep.metadata.processors;

import cz.muni.fi.pb138.webrep.repositories.WarRepository;
import cz.muni.fi.pb138.webrep.util.Pair;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.tomcat.util.http.fileupload.IOUtils;

/**
 *
 * @author Max
 */
public class WarMetadataProcessor {
    public boolean process(String pathToFile) throws IOException {
        //Extract "filename-version" from filepath
        int position = pathToFile.lastIndexOf(File.separator);
        String artifactVersion = pathToFile.substring(position+1);
        String artifactName = pathToFile.substring(0, position);
        position = artifactName.lastIndexOf(File.separator);
        artifactName = artifactName.substring(position+1)+"-"+artifactVersion;
        
        //extracting web.xml from web archive
        //web.xml whould be in pathToFile directory
        //with name x.xml, where x is number of version
        ZipFile zip = new ZipFile(new File(pathToFile));
        List<ZipEntry> entries = new ArrayList<>(Collections.list(zip.entries()));
        for (ZipEntry ze : entries) {
            if (ze.getName().equals("WEB-INF/web.xml")){
             File webxml = new File(pathToFile+".xml");   
             webxml.createNewFile();
             InputStream is = zip.getInputStream(ze);
             FileOutputStream fos = new FileOutputStream(webxml);
             IOUtils.copy(is, fos);
             is.close();
             fos.close();
            }
        }
        
        //Adding listeners and filters to db
        WarRepository rep = new WarRepository();
        rep.addWarRecord(pathToFile, artifactName);
                
        //TESTS
        //System.err.println(rep.getListOfFilters(artifactName));
        //System.err.println(rep.getListOfListeners(artifactName));
        //System.err.println(rep.getByListener("MyListener"));
        //System.err.println(rep.getByFilter("SecondFilter"));
        return true;
    }
    
    //delete all versions of archive metadata
    public void deleteMetadata(String name) throws IOException{
        WarRepository rep = new WarRepository();
        rep.deleteRecord(name);
        
    }
    
    //returns map of archives, which contains each filter/litener in listOfPairs
    public Map<String, Set<Integer>> getArtifactsByMetadata(List<Pair<String, String>> listOfPairs) throws IOException {
        WarRepository rep = new WarRepository();
        return rep.getByMetadata(listOfPairs);
    }
}
